package com.smartcruise.task.newTask;

import com.codbking.calendar.CalendarBean;
import com.smartcruise.data.cruiseData.CruiseContract;
import com.smartcruise.data.cruiseData.CruiseData;
import com.smartcruise.task.TaskAdapter;
import com.smartcruise.util.Key;
import com.smartcruise.util.TempleUser;
import com.smartcruise.util.TimeUtils;

import java.util.Calendar;

import io.realm.RealmResults;

/**
 * Created by FengChaoQun
 * on 2018/1/22 0022
 */

public class NewTaskPresenterImp implements NewTaskContract.Presenter {

    private NewTaskContract.View mView;
    private Calendar today = Calendar.getInstance();

    public NewTaskPresenterImp(NewTaskContract.View mView) {
        this.mView = mView;
        today.set(Calendar.HOUR_OF_DAY, 23);
        today.set(Calendar.MINUTE, 59);
        today.set(Calendar.SECOND, 59);
    }

    @Override
    public void start() {
        buildData(today);
    }

    @Override
    public void getData(CalendarBean calendarBean) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendarBean.year, calendarBean.moth - 1, calendarBean.day);
        buildData(calendar);
    }

    private void buildData(Calendar calendar) {

        if (calendar.equals(today) || calendar.before(today)) {//今天和之前的数据
            //查询本地当天任务
            RealmResults<CruiseData> all = mView.getRealm().where(CruiseData.class)
                    .equalTo(Key.inspectorId, TempleUser.getInstance().getAccount())
                    .equalTo(Key.isCopyData, true)
                    .equalTo(Key.statusInApp, CruiseContract.TASK_IS_WAIT_START)
                    .between(Key.shouldRunDate, TimeUtils.getDayBeginTime(calendar).getTime(), TimeUtils.getDayEndTime(calendar).getTime())
                    .findAll();
            TaskAdapter adapter = new TaskAdapter(mView.getActivity(), all);
            mView.setData(adapter);
        } else {
//            RealmResults<CruiseData> all = mView.getRealm().where(CruiseData.class)
//                    .equalTo(Key.inspectorId, TempleUser.getInstance().getAccount())
//                    .equalTo(Key.isCopyData, false)
//                    .equalTo(Key.isFromNet, false)
//                    .findAll();
//
//            List<CruiseData> cruiseDataList = new ArrayList<>();
//            for (CruiseData cruiseData : all) {
//                List<Date> dates = CronTransfer.computeFireTimesBetween(cruiseData.getCronExpression(),
//                        TimeUtils.getDayBeginTime(calendar), TimeUtils.getDayEndTime(calendar));
//                if (ListUtils.isHaveContent(dates)) {
//                    cruiseDataList.add(cruiseData);
//                }
//            }

//            NewTaskAdapter adapter = new NewTaskAdapter(mView.getActivity(), cruiseDataList, calendar);
//            mView.setData(adapter);

            //test
            CruiseContract.buildTask(calendar, mView.getRealm());
            RealmResults<CruiseData> dataRealmResults = mView.getRealm().where(CruiseData.class)
                    .equalTo(Key.inspectorId, TempleUser.getInstance().getAccount())
                    .equalTo(Key.isCopyData, true)
                    .equalTo(Key.statusInApp, CruiseContract.TASK_IS_WAIT_START)
                    .between(Key.shouldRunDate, TimeUtils.getDayBeginTime(calendar).getTime(), TimeUtils.getDayEndTime(calendar).getTime())
                    .findAll();
            TaskAdapter adapter = new TaskAdapter(mView.getActivity(), dataRealmResults);
            mView.setData(adapter);
        }
    }
}
